//
//  YTUserCommentViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/6.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTUserCommentViewController.h"
#import "YTUserCommentViewModel.h"
#import "YTUserCommentModel.h"
#import "YTUserCommentCell.h"

#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface YTUserCommentViewController ()<
UITableViewDelegate,
UITableViewDataSource,
DZNEmptyDataSetSource,
DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UILabel *carStarText;
@property (weak, nonatomic) IBOutlet UILabel *serviceText;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) YTUserCommentViewModel *viewModel;
@end

@implementation YTUserCommentViewController

- (void)initialize {
    _tableView.tableFooterView = [UIView new];
    _viewModel = [[YTUserCommentViewModel alloc] init];
    @weakify(self);
    [self.tableView addLoadDataControl:^{
        @strongify(self);
        [self.viewModel.loadMoreDataCommand execute:nil];
    }];
    
    [self.tableView addRefreshControl:^{
        @strongify(self);
        [self.viewModel.requestRemoteDataCommand execute:nil];
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [[_viewModel.requestRemoteDataCommand.executionSignals switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView reloadData];
    }];
    
    [[_viewModel.loadMoreDataCommand.executionSignals switchToLatest] subscribeNext:^(NSArray<YTUserCommentModel *> *data) {
        @strongify(self);
        if (data.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView reloadData];
    }];
    
    RAC(_carStarText, text) = RACObserve(_viewModel, carStar);
    RAC(_serviceText, text) = RACObserve(_viewModel, serviceStar);
    [_viewModel.requestRemoteDataCommand execute:nil];
}

#pragma mark - UITableViewDeleagte

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTUserCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTUserCommentCell" forIndexPath:indexPath];
    cell.model = _viewModel.dataSource[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _viewModel.dataSource.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == (_viewModel.dataSource.count - 5)) {
        [tableView.mj_footer beginRefreshing];
    }
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"您还未获得相关评价哦~";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                 NSForegroundColorAttributeName: kGrayColor};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"empy_comment"];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

@end
